//
//  OilCradCreateCell.m
//  Base_App
//
//  Created by Yutian Duan on 2019/6/17.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "OilCradCreateCell.h"

@interface OilCradCreateCell () <UITextFieldDelegate>

@end

@implementation OilCradCreateCell

- (void)awakeFromNib {
  [super awakeFromNib];
  // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  [super setSelected:selected animated:animated];
  
  // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
    self.contentView.backgroundColor = HEXCOLOR(0xf7f8fc);
    [self crateViews];
  }
  return self;
}

- (void)crateViews {
  
  CGFloat labelH = 35;
  
  _titleLabel = [[UILabel alloc] init];
  _titleLabel.textAlignment = NSTextAlignmentLeft;
  _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
  _titleLabel.textColor = DEF_BLACK;
  [self.contentView addSubview:_titleLabel];
  
  _cardField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 90 - 100, labelH)];
  [_cardField setTextFont:[UIFont systemFontOfSize:14]];
  [_cardField set_placeTextColor:DEF_GRAY];
  _cardField.delegate = self;
  _cardField.placeholder = YTLocalizedString(@"输入已登记手机号码");
  _cardField.layer.masksToBounds = YES;
  _cardField.layer.cornerRadius = 8;
  _cardField.layer.borderWidth = 0.8;
  _cardField.layer.borderColor = COLOR_SEPARATOR.CGColor;
  _cardField.backgroundColor = [UIColor whiteColor];
  _cardField.leftViewMode = UITextFieldViewModeAlways;
  _cardField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, labelH)];
  [self.contentView addSubview:_cardField];
  
  _addButton = [[UIButton alloc] init];
  [_addButton setImage:[UIImage imageNamed:@"Oil_list_delete"] forState:UIControlStateNormal];
  [_addButton setImage:[UIImage imageNamed:@"Oil_list_add"] forState:UIControlStateSelected];
  [_addButton addTarget:self action:@selector(addRowClick:) forControlEvents:UIControlEventTouchUpInside];
//  _addButton.backgroundColor = [UIColor orangeColor];
  [self.contentView addSubview:_addButton];
  
  
  CGSize buttonSize = CGSizeZero;
  
  //! 汽油
  UIButton *gasolineButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
  gasolineButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
  [gasolineButton setTitle:YTLocalizedString(@"汽油") forState:UIControlStateNormal];
  [gasolineButton setImage:[UIImage imageNamed:@"Oil_CheckImage_0"] forState:UIControlStateNormal];
  [gasolineButton setImage:[UIImage imageNamed:@"Oil_CheckImage_1"] forState:UIControlStateSelected];
  [gasolineButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [self.contentView addSubview:gasolineButton];
  gasolineButton.tag = 100;
  [gasolineButton addTarget:self action:@selector(checkType:) forControlEvents:UIControlEventTouchUpInside];
  [gasolineButton sizeToFit];
  buttonSize = CGSizeMake(gasolineButton.yt_width, 30);
  
  //! 公司
  UIButton *dieselButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
  dieselButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
  [dieselButton setTitle:YTLocalizedString(@"柴油") forState:UIControlStateNormal];
  [dieselButton setImage:[UIImage imageNamed:@"Oil_CheckImage_0"] forState:UIControlStateNormal];
  [dieselButton setImage:[UIImage imageNamed:@"Oil_CheckImage_1"] forState:UIControlStateSelected];
  [dieselButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [self.contentView addSubview:dieselButton];
  dieselButton.tag = 101;
  [dieselButton addTarget:self action:@selector(checkType:) forControlEvents:UIControlEventTouchUpInside];
  _buttonArray = @[gasolineButton,dieselButton];
  
  _lineView = [[UIView alloc] init];
  _lineView.backgroundColor = DEF_GRAY;
  [self.contentView addSubview:_lineView];
  
  [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.mas_equalTo(self.contentView.mas_leading).offset(30);
    make.top.mas_equalTo(self.contentView.mas_top).offset(15);
    make.height.mas_equalTo(@(labelH));
    make.width.mas_equalTo(@85);
  }];
  
  [_cardField mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.mas_equalTo(self.titleLabel.mas_trailing);
    make.height.top.mas_equalTo(self.titleLabel);
    make.trailing.mas_equalTo(self.contentView.mas_trailing).offset(-100);
  }];
  
  [_addButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.mas_equalTo(self.contentView);
    make.trailing.mas_equalTo(self.contentView.mas_trailing).offset(-20);
    make.size.mas_equalTo(CGSizeMake(60, 60));
  }];
  
  
  [gasolineButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.mas_equalTo(self.cardField.mas_leading);
    make.top.mas_equalTo(self.cardField.mas_bottom).offset(10);
    make.width.mas_equalTo(buttonSize.width);
    make.height.mas_equalTo(buttonSize.height);
  }];
  
  [dieselButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.mas_equalTo(gasolineButton.mas_trailing).offset(10);
    make.top.width.height.mas_equalTo(gasolineButton);
  }];
  
  
  [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.mas_equalTo(self.contentView.mas_leading).offset(10);
    make.trailing.mas_equalTo(self.contentView.mas_trailing).offset(-10);
    make.bottom.mas_equalTo(self.contentView.mas_bottom);
    make.height.mas_equalTo(@0.8);
  }];
  
}

- (void)setModel:(OilCradCreateModel *)model {
  _model = model;
  [self setupData];
}

- (void)setupData {
  
  _titleLabel.text = [NSString stringWithFormat:@"%@ %zd",YTLocalizedString(@"车辆资料"),_model.row+1];
  
  if (_model.isGasoline) {
    _buttonArray[0].selected = YES;
    _buttonArray[1].selected = NO;
  } else {
    _buttonArray[0].selected = NO;
    _buttonArray[1].selected = YES;
  }
  
  if (_model.row == 0) {
    _addButton.selected = YES;
  } else {
    _addButton.selected = NO;
  }
  
}

- (void)checkType:(UIButton *)sender {
  
  sender.selected = YES;

  if (sender.tag == 100) {
    _model.isGasoline = YES;
    _buttonArray[1].selected = NO;
  } else {
    _model.isGasoline = NO;
    _buttonArray[0].selected = NO;
  }
  
}

- (void)addRowClick:(UIButton *)sender {
  [_cardField endEditing:YES];
  if (self.addCellBlock) {
    self.addCellBlock(sender.selected);
  }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
  _model.cardNumb = textField.text;
}

@end

